package managedbean.SCM.Supplier;

import entity.RawMaterial;
import entity.SCM.Supplier;
import entity.SCM.SupplyItem;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javaclass.EmailValidator;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import session.stateless.SCM.SupplierBeanLocal;

@ManagedBean(name="addSupplier")
@ViewScoped
public class AddSupplierManagedBean implements Serializable {

    public AddSupplierManagedBean() {
    }
    @EJB
    private SupplierBeanLocal supplierBeanLocal;
    private List<SupplyItem> supplyItemList = new ArrayList<SupplyItem>();
    private String statusMessage;
    private List<String> materials  = new ArrayList<String>();
    private String supplierName;
    private String supplierAddress;
    private String supplierPostcode;
    private String supplierContactNo;
    private String supplierEmail;
    private SupplyItem newSi = new SupplyItem();

    public SupplyItem getNewSi() {
        return newSi;
    }

    public void setNewSi(SupplyItem newSi) {
        this.newSi = newSi;
    }

    public String getSupplierAddress() {
        return supplierAddress;
    }

    public void setSupplierAddress(String supplierAddress) {
        this.supplierAddress = supplierAddress;
    }

    public String getSupplierContactNo() {
        return supplierContactNo;
    }

    public void setSupplierContactNo(String supplierContactNo) {
        this.supplierContactNo = supplierContactNo;
    }

    public String getSupplierEmail() {
        return supplierEmail;
    }

    public void setSupplierEmail(String supplierEmail) {
        this.supplierEmail = supplierEmail;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public String getSupplierPostcode() {
        return supplierPostcode;
    }

    public void setSupplierPostcode(String supplierPostcode) {
        this.supplierPostcode = supplierPostcode;
    }

    public List<String> getMaterials() {
        List<RawMaterial> rms = supplierBeanLocal.getAllMaterial();
        if(rms!=null){
            for(int i=0;i<rms.size();i++){
                this.materials.add(rms.get(i).getMaterialType());
            }
        }
        return materials;
    }

    public List<SupplyItem> getSupplyItemList() {
        return supplyItemList;
    }
    
    public void addSupplyItem(ActionEvent event){
        SupplyItem temp = this.newSi;
        this.supplyItemList.add(temp);
        newSi = new SupplyItem();
    }

    public void saveSupplier(ActionEvent event){
        try{
            if(supplyItemList==null){
                statusMessage = "Pls add Supply Item first";
            }else{
                System.out.printf("44444444444444");
                String name = supplierBeanLocal.createSupplier(supplierName, supplierAddress, 
                        supplierPostcode, supplierContactNo, supplierEmail,supplyItemList);
                if(name!=null){
                    statusMessage = "Success";
                }else{
                    statusMessage = "Unsuccess";
                }
            }
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, 
                    "Add Supplier Result: " + statusMessage , ""));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
     public void validateEmail(FacesContext context, UIComponent toValidate, Object value){     
        String email = (String) value;        
        EmailValidator emailValidator = new EmailValidator();
        boolean check = emailValidator.validate(email);
        if (!check) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Email is not correct. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    } 
     
    public void validateContactNumber(FacesContext context, UIComponent toValidate, Object value){       
        String contactNumber = (String) value;
        boolean check = true;        
        if(contactNumber.length()>15 || contactNumber.length()<8){
            check=false;
        }
        try {
            Integer.parseInt(contactNumber);
        }catch (NumberFormatException nfe){
            check = false;
        }
        if (!check) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Contact number is not correct. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
    public void validateUniqueUser(FacesContext context, UIComponent toValidate, Object value){        
        String userName = (String) value;
        Supplier supplier = supplierBeanLocal.getSupplier(userName); 
        if (supplier != null) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Username is not unique. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }
    }
    
    public void validatePostCode(FacesContext context, UIComponent toValidate, Object value){
        String postCode = (String) value;
        boolean check = true;
        if(postCode.length()!=6){
            check = false;
        }
        try {
            Integer.parseInt(postCode);
        }catch (NumberFormatException nfe){
            check = false;
        }
        if (!check) {
            ((UIInput)toValidate).setValid(false);
            FacesMessage message = new FacesMessage("Post code is not correct. Please re-enter.");
            message.setSeverity(FacesMessage.SEVERITY_WARN);
            context.addMessage(toValidate.getClientId(context), message);
        }        
    }
    
}
